package serializer;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import model.ListWords;
import model.Word;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class SerializerXml {
	
	private Document document;
	String file;

	public SerializerXml(String file) {
		this.file=file;
		SAXBuilder sxb = new SAXBuilder();
		try{
			document = new Document(new Element("wor"));
		}
		catch(Exception e){
					
		}
	}
	
    public boolean store(List<Word> words) {
    	
    	try {
			File f = new File(file);
			System.out.println("OK");
			//f.delete();
		} catch (Exception e) {
			// TODO: handle exception
		}

    	
    	Iterator<Word> iterListWord=words.iterator();
    	
    	while(iterListWord.hasNext()) {
    		Word  currentWord=iterListWord.next();
    		Element word=currentWord.parseXML();
    	    document.getRootElement().addContent(word);			
    	}
    	return saveWordXml();
    	
    }
    
    private boolean saveWordXml() {
    	try {
    	    XMLOutputter fichierServeur = new XMLOutputter(Format.getPrettyFormat());
    	    fichierServeur.output(document, new FileOutputStream(file));
    	}catch (java.io.IOException e){
    	    System.out.println("La sauvegarde du fichier serveur.xml a échoué."); 
    	    return false;
    	}
    	return true;
    }
    
public  boolean load(ListWords lw) {
		
		SAXBuilder sxb = new SAXBuilder();

		try {
			document = sxb.build(new File(file));
		} catch (Exception e) {
			System.out.println("Erreur lors de l'ouverture du fichier !");
			return false;
		}

		Element racine = document.getRootElement();
		
		List<Element> wordList=racine.getChildren("word");
		
		Iterator<Element> itWords = wordList.iterator();
		
		for(Element currentWord:wordList) {
			String name = currentWord.getAttributeValue("nom");
			Element racineSyllables=currentWord.getChild("syllables");			
			
			List<Element> SyllableList=racineSyllables.getChildren("syllable");	
			ArrayList<String> syllables=new ArrayList<String>();
			
			for(Element currentSyllable:SyllableList) {				
				syllables.add(currentSyllable.getText());					
			}
			Word wo;		
			wo = new Word(name, syllables);
			lw.addWord(wo);
			
		}
		
		return true;
	}

	/**
	 * add a word into the xml file.
	 * @param word
	 * 			the word to add.
	 * @return true if the words is added successful, false otherwise 
	 */
	public boolean addWordXml(Word word) {
		document.getRootElement().addContent(word.parseXML());
		return saveWordXml();
	}
	
	public boolean removeWordXml(String wordDescription) {
		
		boolean notFound=true;
		
		SAXBuilder sxb = new SAXBuilder();

		try {
			document = sxb.build(new File(file));
		} catch (Exception e) {
			System.out.println("Erreur lors de l'ouverture du fichier !");
			return false;
		}
		
		Element racine = document.getRootElement();
		
		List<Element> wordList=racine.getChildren("word");
		
		Iterator<Element> itWords = wordList.iterator();
		
		while(itWords.hasNext()) {
			Element currentWord=itWords.next();
			if(currentWord.getAttributeValue("nom").equals(wordDescription)) {
				racine.removeContent(currentWord);
				return saveWordXml();
			}			
		}

		return false;
	}
	
	
    public static void main(String args[]) {
    	SerializerXml ser=new SerializerXml("words");
    	ListWords myListe=new ListWords("acteurs.txt");
		System.out.println(myListe.size());
		System.out.println(ser.store(myListe.getListWord()));
		
		Word w=new Word("toyota");
		w.addSyllable("toyo");
		w.addSyllable("ta");
		ser.addWordXml(w);
		ser.removeWordXml("toyota");
		
		ListWords myList=new ListWords();
		//System.out.println(ser.load(myList));
		//System.out.println(myList.size());
		
		
    }
}
